import 'package:flutter/services.dart';

/// 权限状态枚举
enum PermissionStatus {
  /// 权限已授予
  granted,

  /// 权限被拒绝
  denied,

  /// 权限被永久拒绝
  permanentlyDenied,

  /// 权限状态未知
  unknown,
}

/// SMS权限管理类
///
/// 负责处理Android平台上短信相关权限的申请、检查和管理
class SmsPermissions {
  static const MethodChannel _channel =
      MethodChannel('sms_listener/permissions');

  /// SMS接收相关权限列表
  static const List<String> _smsReceivePermissions = [
    'android.permission.RECEIVE_SMS',
    'android.permission.READ_SMS',
  ];

  /// SMS发送相关权限列表
  static const List<String> _smsSendPermissions = [
    'android.permission.SEND_SMS',
  ];

  /// 所有SMS权限列表
  static const List<String> _allSmsPermissions = [
    'android.permission.RECEIVE_SMS',
    'android.permission.READ_SMS',
    'android.permission.SEND_SMS',
  ];

  /// 请求SMS接收权限
  ///
  /// 返回值：
  /// - true: 所有必需权限都已获得
  /// - false: 部分或全部权限被拒绝
  static Future<bool> requestSmsPermissions() async {
    try {
      final bool result = await _channel.invokeMethod('requestSmsPermissions');
      return result;
    } on PlatformException catch (e) {
      print('请求SMS权限时发生错误: ${e.message}');
      return false;
    }
  }

  /// 请求SMS发送权限
  ///
  /// 返回值：
  /// - true: 发送权限已获得
  /// - false: 发送权限被拒绝
  static Future<bool> requestSmsSendPermission() async {
    try {
      final bool result =
          await _channel.invokeMethod('requestSmsSendPermission');
      return result;
    } on PlatformException catch (e) {
      print('请求SMS发送权限时发生错误: ${e.message}');
      return false;
    }
  }

  /// 请求所有SMS权限（接收和发送）
  ///
  /// 返回值：
  /// - true: 所有权限都已获得
  /// - false: 部分或全部权限被拒绝
  static Future<bool> requestAllSmsPermissions() async {
    try {
      final bool result =
          await _channel.invokeMethod('requestAllSmsPermissions');
      return result;
    } on PlatformException catch (e) {
      print('请求所有SMS权限时发生错误: ${e.message}');
      return false;
    }
  }

  /// 检查是否已有SMS接收权限
  ///
  /// 返回值：
  /// - true: 所有必需权限都已获得
  /// - false: 缺少部分或全部权限
  static Future<bool> hasSmsPermissions() async {
    try {
      final bool result = await _channel.invokeMethod('hasSmsPermissions');
      return result;
    } on PlatformException catch (e) {
      print('检查SMS权限时发生错误: ${e.message}');
      return false;
    }
  }

  /// 检查是否已有SMS发送权限
  ///
  /// 返回值：
  /// - true: 发送权限已获得
  /// - false: 发送权限未获得
  static Future<bool> hasSmsSendPermission() async {
    try {
      final bool result = await _channel.invokeMethod('hasSmsSendPermission');
      return result;
    } on PlatformException catch (e) {
      print('检查SMS发送权限时发生错误: ${e.message}');
      return false;
    }
  }

  /// 检查是否已有所有SMS权限（接收和发送）
  ///
  /// 返回值：
  /// - true: 所有权限都已获得
  /// - false: 缺少部分或全部权限
  static Future<bool> hasAllSmsPermissions() async {
    try {
      final bool result = await _channel.invokeMethod('hasAllSmsPermissions');
      return result;
    } on PlatformException catch (e) {
      print('检查所有SMS权限时发生错误: ${e.message}');
      return false;
    }
  }

  /// 检查单个权限状态
  ///
  /// [permission] 要检查的权限名称
  ///
  /// 返回值：
  /// - true: 权限已授予
  /// - false: 权限未授予
  static Future<bool> hasPermission(String permission) async {
    try {
      final bool result = await _channel.invokeMethod('hasPermission', {
        'permission': permission,
      });
      return result;
    } on PlatformException catch (e) {
      print('检查权限 $permission 时发生错误: ${e.message}');
      return false;
    }
  }

  /// 请求单个权限
  ///
  /// [permission] 要请求的权限名称
  ///
  /// 返回值：
  /// - true: 权限请求成功
  /// - false: 权限请求失败
  static Future<bool> requestPermission(String permission) async {
    try {
      final bool result = await _channel.invokeMethod('requestPermission', {
        'permission': permission,
      });
      return result;
    } on PlatformException catch (e) {
      print('请求权限 $permission 时发生错误: ${e.message}');
      return false;
    }
  }

  /// 检查权限是否被永久拒绝
  ///
  /// [permission] 要检查的权限名称
  ///
  /// 返回值：
  /// - true: 权限被永久拒绝（用户选择了"不再询问"）
  /// - false: 权限未被永久拒绝
  static Future<bool> isPermissionPermanentlyDenied(String permission) async {
    try {
      final bool result =
          await _channel.invokeMethod('isPermissionPermanentlyDenied', {
        'permission': permission,
      });
      return result;
    } on PlatformException catch (e) {
      print('检查权限永久拒绝状态时发生错误: ${e.message}');
      return false;
    }
  }

  /// 打开应用设置页面
  ///
  /// 当权限被永久拒绝时，可以引导用户到设置页面手动开启权限
  static Future<bool> openAppSettings() async {
    try {
      final bool result = await _channel.invokeMethod('openAppSettings');
      return result;
    } on PlatformException catch (e) {
      print('打开应用设置时发生错误: ${e.message}');
      return false;
    }
  }

  /// 获取所有必需的SMS权限列表
  static List<String> get requiredPermissions =>
      List.unmodifiable(_allSmsPermissions);

  /// 获取权限状态的详细信息
  ///
  /// [permission] 要检查的权限名称
  ///
  /// 返回权限的详细状态
  static Future<PermissionStatus> getPermissionStatus(String permission) async {
    try {
      final int status = await _channel.invokeMethod('getPermissionStatus', {
        'permission': permission,
      });

      switch (status) {
        case 0:
          return PermissionStatus.granted;
        case 1:
          return PermissionStatus.denied;
        case 2:
          return PermissionStatus.permanentlyDenied;
        default:
          return PermissionStatus.unknown;
      }
    } on PlatformException catch (e) {
      print('获取权限状态时发生错误: ${e.message}');
      return PermissionStatus.unknown;
    }
  }
}
